
export const GET_ADDRESS = 'GET_ADDRESS' //获取地址
export const DELETE_ADDRESS = 'DELETE_ADDRESS' //删除地址
export const ADD_ADDRESS = 'ADD_ADDRESS' //添加地址
export const updata_ADDRESS = 'updata_ADDRESS'  //编辑地址
export const CHANGE_CHECK = 'CHANGE_CHECK'  //改变默认地址
export const IS_UPDAADDRESSTELIST = 'IS_UPDAADDRESSTELIST'  //是否需要刷新列表



export const SELECT_ADDRESS = 'SELECT_ADDRESS'  //发起订单时收货地址


export const UPDATESELECTFROM = 'UPDATESELECTFROM'  //发起订单时收货地址



export default{
    state: {
        addreList:JSON.parse(window.localStorage.getItem('address'))||{},
        selectAddress:{},
        selectForm: null,
        isUpdateList:{type:true}
    },
    mutations:{
        [GET_ADDRESS](state, data) {
            state.addreList = data;
            localStorage.setItem('address', JSON.stringify(state.addreList))
        },
        [CHANGE_CHECK](state, id) {
            state.addreList.content.map((v,i)=>{
                v.isDefault = 0;
                if(v.deliveryId === id){
                    v.isDefault = 1;
                }
            })
            localStorage.setItem('address', JSON.stringify(state.addreList))
        },
        [DELETE_ADDRESS](state, id) {
            state.addreList.content =  state.addreList.content.filter((v)=> v.deliveryId !== id)
            if(state.selectAddress != {} && id == state.selectAddress.deliveryId){
                state.selectAddress = {}
            }
            localStorage.setItem('address', JSON.stringify(state.addreList))
        },
        [ADD_ADDRESS](state, data) {
            state.addreList.content.push(data);
            localStorage.setItem('address', JSON.stringify(state.addreList))
        },
        [updata_ADDRESS](state, data) {
            state.addreList.content.map((v,i)=>{
                if(data.isDefault == 1){
                    v.isDefault = 0;
                }
                if(v.deliveryId === data.deliveryId){
                    v = data;
                }
            })
            localStorage.setItem('address', JSON.stringify(state.addreList))
        },
        [SELECT_ADDRESS](state, data) {
            state.selectAddress = data ;
        },
        [IS_UPDAADDRESSTELIST](state, data) {
            state.isUpdateList ={type: data};
        },
      [UPDATESELECTFROM](state,data) {
            console.log(data)
            state.selectForm = data
      }
    },
    actions:{

        [CHANGE_CHECK]({commit}, id) {

            commit(CHANGE_CHECK, id)
        },
        [GET_ADDRESS]({commit}, id) {

            commit(GET_ADDRESS, id)
        },
        [DELETE_ADDRESS]({commit}, id) {

            commit(DELETE_ADDRESS, id)
        },
        [ADD_ADDRESS]({commit},data) {

            commit(ADD_ADDRESS,data)
        },
        [updata_ADDRESS]({commit},data) {

            commit(updata_ADDRESS,data)
        },
        [SELECT_ADDRESS]({commit},data) {

            commit(SELECT_ADDRESS,data)
        },
        [IS_UPDAADDRESSTELIST]({commit},data) {

            commit(IS_UPDAADDRESSTELIST,data)
        },
      [UPDATESELECTFROM]({commit},data) {

            commit(UPDATESELECTFROM,data)
        },
    }

}